package com.biu.billiards.task;
import com.biu.billiards.model.bo.BilliardsBO;
import com.biu.billiards.model.entity.Shop;
import com.biu.billiards.model.entity.User;
import com.biu.billiards.model.vo.billiards.GetBilliardsVo;
import com.biu.billiards.model.vo.mqtt.OpenBilliardsVo;
import com.biu.billiards.model.vo.withdraw.AddWithdrawVo;
import com.biu.billiards.service.IBilliardsService;
import com.biu.billiards.service.IDeviceService;
import com.biu.billiards.service.IShopService;
import com.biu.billiards.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 定时延时
 */
@Slf4j
@Component
public class TimedTask {

    @Resource
    private IBilliardsService billiardsService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IUserService userService;

    @Resource
    private IShopService shopService;

//    @Scheduled(cron = "0 * * * * ?")
    @Scheduled(cron = "0 0 * * * ?")
    public void delay(){
        List<Shop> list = shopService.selectShopList();
        for (Shop shop:list){
            deviceService.taskOpen(true,shop.getShopId());
            deviceService.taskOpen(false,shop.getShopId());
        }

    }



 //   @Scheduled(cron = "0 30 9 * * ?")
 @Scheduled(cron = "0 30 9 ? * MON")
     public void delay16(){

        System.out.println("运行了嗷嗷");

        List<Shop> list = shopService.selectShopList();

        for (Shop shop:list){

            if(shop.getWallet().compareTo(new BigDecimal(0)) == 1){

                System.out.println("a大于b");

                AddWithdrawVo addWithdrawVo = new AddWithdrawVo();
                addWithdrawVo.setUserId(shop.getShopId().toString());
                addWithdrawVo.setUserName(shop.getBankCardName());
                addWithdrawVo.setBankCard(shop.getBankCard());
                addWithdrawVo.setUserPhone(shop.getShopPhone());
                addWithdrawVo.setBankType(shop.getBankType());
                addWithdrawVo.setUserType(2);
                addWithdrawVo.setWithdrawPrice(shop.getWallet());
                shopService.autoWithdraw(addWithdrawVo);

            }


            if ( shop.getRecommendId()!=null && shop.getRecommendId()!=0){
             User user =userService.getUser(shop.getRecommendId());

             if (user!=null){

                 if(user.getWallet().compareTo(new BigDecimal(0)) == 1){

                     AddWithdrawVo addWithdrawVo2 = new AddWithdrawVo();

                     addWithdrawVo2.setUserId(user.getUserId().toString());
                     addWithdrawVo2.setUserName(user.getBankCardName());
                     addWithdrawVo2.setBankCard(user.getBankCard());
                     addWithdrawVo2.setUserPhone(user.getPhone());
                     addWithdrawVo2.setBankType(user.getBankType());
                     addWithdrawVo2.setUserType(1);
                     addWithdrawVo2.setWithdrawPrice(user.getWallet());
                     shopService.autoWithdraw(addWithdrawVo2);

                 }



             }




            }


        }






    }









    @Scheduled(cron = "*/7 * * * * *")
    public void FixDelay(){


        GetBilliardsVo getBilliardsVo = new GetBilliardsVo();

        System.out.println("7秒定时检测");

      //  getBilliardsVo.setShopId(2);

        List<BilliardsBO> boList = billiardsService.getBilliardsList(getBilliardsVo);

        List<OpenBilliardsVo> openBilliardsVoList =  new ArrayList<>();

        for (BilliardsBO billiardsBO :boList){

            if (billiardsBO.getBilliardsStatus()==1){


                if (LocalDateTime.now().compareTo(billiardsBO.getEndTime())>0){

                    System.out.println(billiardsBO.getBilliardsNumber()+"号台时间到期，正在关闭");

                    OpenBilliardsVo openBilliardsVo = new OpenBilliardsVo();
                    openBilliardsVo.setBilliardsId(billiardsBO.getBilliardsId());
                    openBilliardsVo.setOpen(0);
                    openBilliardsVo.setBilliardsNumber(billiardsBO.getBilliardsNumber());
                    openBilliardsVo.setDeviceNumber(billiardsBO.getDeviceNumber());
                    openBilliardsVo.setShopId(billiardsBO.getShopId());
                    openBilliardsVo.setHardwareId(billiardsBO.getHardwareId());

                    openBilliardsVoList.add(openBilliardsVo);

                    //billiardsService.openBilliards(openBilliardsVo);





                }

            }

        }


        if (openBilliardsVoList.size()!=0){

            System.out.println("开始关台了");
            System.out.println("总共要关"+openBilliardsVoList.size()+"个");

            billiardsService.openBilliardsList(openBilliardsVoList);


        }


    }









}
